// Copyright (C) 2012 by Johann Duscher <jonny.dee@gmx.net>
// 
// This file is part of barcodeart-pp.
//
// barcodeart-pp is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// barcodeart-pp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with barcodeart-pp. If not, see <http://www.gnu.org/licenses/>.

#include "EncoderConfigView.hpp"
#include "ui_EncoderConfigView.h"

#include <barcodeart/qrcode/EncoderConfig.hpp>


namespace barcodeart
{

namespace qrcode
{

namespace qrencode
{

EncoderConfigView::EncoderConfigView(IEncoderPluginContext& pluginContext, QWidget *parent) :
    QWidget(parent),
    m_pPluginContext(&pluginContext),
    m_pUI(new Ui::EncoderConfigView)
{
    m_pUI->setupUi(this);

    m_errorCorrectionLevelStringMap[ErrorCorrection::LOW] = tr("Low");
    m_errorCorrectionLevelStringMap[ErrorCorrection::MEDIUM] = tr("Medium");
    m_errorCorrectionLevelStringMap[ErrorCorrection::QUALITY] = tr("Quality");
    m_errorCorrectionLevelStringMap[ErrorCorrection::HIGH] = tr("High");
    m_pUI->errorCorrectionLevelComboBox->addItems(m_errorCorrectionLevelStringMap.values());

    m_encodingTypeStringMap[Encoding::ALPHANUMERIC] = tr("Alphanumeric");
    m_encodingTypeStringMap[Encoding::NUMERIC] = tr("Numeric");
    m_encodingTypeStringMap[Encoding::KANJI] = tr("Kanji");
    m_encodingTypeStringMap[Encoding::BINARY] = tr("Binary");
    m_pUI->encodingTypeComboBox->addItems(m_encodingTypeStringMap.values());
}

EncoderConfigView::~EncoderConfigView()
{
    delete m_pUI;
}


void EncoderConfigView::setConfig(const IEncoderConfig& iConfig)
{
    const EncoderConfig& config = dynamic_cast<const EncoderConfig&>(iConfig);

    m_pUI->errorCorrectionLevelComboBox->setCurrentIndex(m_pUI->errorCorrectionLevelComboBox->findText(toErrorCorrectionString(config.getErrorCorrectionLevel())));
    m_pUI->encodingTypeComboBox->setCurrentIndex(m_pUI->encodingTypeComboBox->findText(toEncodingString(config.getEncodingType())));
    m_pUI->caseSensitiveCheckBox->setChecked(config.isCaseSensitive());
    m_pUI->versionSpinBox->setValue(config.getVersion());
}

void EncoderConfigView::getConfig(IEncoderConfig& iConfig) const
{
    EncoderConfig& config = dynamic_cast<EncoderConfig&>(iConfig);

    config.setErrorCorrectionLevel(toErrorCorrectionLevel(m_pUI->errorCorrectionLevelComboBox->currentText()));
    config.setEncodingType(toEncodingType(m_pUI->encodingTypeComboBox->currentText()));
    config.setCaseSensitive(m_pUI->caseSensitiveCheckBox->isChecked());
    config.setVersion(m_pUI->versionSpinBox->value());
}

void EncoderConfigView::on_versionSpinBox_valueChanged(int value)
{
    m_pPluginContext->notifyConfigViewChanged(this);
}

void EncoderConfigView::on_caseSensitiveCheckBox_stateChanged(int state)
{
    m_pPluginContext->notifyConfigViewChanged(this);
}

void EncoderConfigView::on_errorCorrectionLevelComboBox_currentIndexChanged(int index)
{
    m_pPluginContext->notifyConfigViewChanged(this);
}

void EncoderConfigView::on_encodingTypeComboBox_currentIndexChanged(int index)
{
    m_pPluginContext->notifyConfigViewChanged(this);
}

ErrorCorrection::Level EncoderConfigView::toErrorCorrectionLevel(const QString& value) const
{
    for (ErrorCorrectionLevelStringMap::const_iterator it = m_errorCorrectionLevelStringMap.constBegin(); it != m_errorCorrectionLevelStringMap.constEnd(); it++)
        if (it.value() == value)
            return it.key();
    return ErrorCorrection::LOW;
}

QString EncoderConfigView::toErrorCorrectionString(ErrorCorrection::Level value) const
{
    return m_errorCorrectionLevelStringMap[value];
}


Encoding::Type EncoderConfigView::toEncodingType(const QString& value) const
{
    for (EncodingTypeStringMap::const_iterator it = m_encodingTypeStringMap.constBegin(); it != m_encodingTypeStringMap.constEnd(); it++)
        if (it.value() == value)
            return it.key();
    return Encoding::ALPHANUMERIC;
}

QString EncoderConfigView::toEncodingString(Encoding::Type value) const
{
    return m_encodingTypeStringMap[value];
}

}

}

}
